<?php

require_once 'config.php';

require_once 'includes/functions.php';

// 在详情页的PHP部分添加

$relatedApps = getRelatedApps($appId, 4);

// 检查是否有应用ID参数

if (!isset($_GET['id'])) {

    header('Location: index.php');

    exit;

}

$appId = $_GET['id'];

$app = getAppById($appId);

// 如果应用不存在，跳转到首页

if (!$app) {

    header('Location: index.php');

    exit;

}

$apps = getApps();

?>

<?php include 'includes/header.php'; ?>

<div style="max-width: 800px; margin: 0 auto;">

    <!-- 返回按钮 -->

    <div style="margin-bottom: 20px;">

        <a href="index.php" class="btn btn-secondary" style="text-decoration: none;">← 返回应用列表</a>

    </div>

    <!-- 应用详情卡片 -->

    <div class="app-card" style="margin-bottom: 30px;">

        <div style="display: flex; align-items: flex-start; gap: 20px; margin-bottom: 20px;">

            <?php if (!empty($app['icon'])): ?>

                <img src="<?php echo htmlspecialchars($app['icon']); ?>" 

                     alt="<?php echo htmlspecialchars($app['name']); ?>" 

                     class="app-icon" style="width: 80px; height: 80px;">

            <?php endif; ?>

            <div style="flex: 1;">

                <h1 style="margin: 0 0 10px 0; color: #333;"><?php echo htmlspecialchars($app['name']); ?></h1>

                <div style="color: #666; margin-bottom: 10px;">

                    <strong>版本:</strong> <?php echo htmlspecialchars($app['version']); ?>

                </div>

                <div style="color: #666;">

                    <strong>更新时间:</strong> <?php echo htmlspecialchars($app['create_time']); ?>

                </div>

            </div>

        </div>

        <div style="margin-bottom: 20px;">

            <a href="<?php echo htmlspecialchars($app['download_url']); ?>" 

               class="btn" 

               style="padding: 12px 30px; font-size: 16px; display: inline-block;"

               target="_blank">

                立即下载

            </a>

        </div>

    </div>

    <!-- 应用介绍 -->

    <div class="app-card" style="margin-bottom: 20px;">

        <h3 style="margin-bottom: 15px; color: #333; border-bottom: 2px solid #ff9a9e; padding-bottom: 5px;">应用介绍</h3>

        <div style="line-height: 1.8; color: #555;">

            <?php echo nl2br(htmlspecialchars($app['description'])); ?>

        </div>

    </div>

    <!-- 更新内容 -->

    <div class="app-card">

        <h3 style="margin-bottom: 15px; color: #333; border-bottom: 2px solid #ff9a9e; padding-bottom: 5px;">更新内容</h3>

        <div style="line-height: 1.8; color: #555;">

            <?php echo nl2br(htmlspecialchars($app['update_content'])); ?>

        </div>

    </div>

    <!-- 管理按钮（仅管理员可见） -->

    <?php if (isLoggedIn()): ?>

        <div class="app-card" style="background: #f8f9fa; margin-top: 20px;">

            <h3 style="margin-bottom: 15px; color: #333;">管理操作</h3>

            <div>

                <a href="admin.php?edit=<?php echo $app['id']; ?>" class="btn btn-secondary">编辑应用</a>

                <form action="action/delete_app.php" method="POST" style="display: inline;">

                    <input type="hidden" name="id" value="<?php echo $app['id']; ?>">

                    <button type="submit" class="btn btn-danger" onclick="return confirm('确定删除这个应用吗？')">删除应用</button>

                </form>

            </div>

        </div>

    <?php endif; ?>

</div>

<?php include 'includes/footer.php'; ?>